﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2011-11-23 17:29:27Z</CreationDate>
</File>
*/
#endregion

using System.Threading.Tasks;

using Outcoder.ComponentModel;
using Outcoder.Messaging;
using Outcoder.Services;

namespace Outcoder.SettingsModel
{
	public class BindableSettingResolver : ObservableObject, IMessageSubscriber<SettingChangeEventArgs>
	{
		ISettingsService settingsService;

		public object this[string key]
		{
			get
			{
				if (settingsService == null)
				{
					settingsService = Dependency.Resolve<ISettingsService>();
					var messageBus = Dependency.Resolve<IMessenger>();
					messageBus.Subscribe(this);
				}
				return settingsService.GetSetting<object>(key, null);
			}
		}

		public Task ReceiveMessage(SettingChangeEventArgs message)
		{
			base.OnPropertyChanged(string.Empty);

#if !SILVERLIGHT
			return Task.FromResult(0);
#else
			var taskSource = new TaskCompletionSource<int>();
			taskSource.SetResult(0);
			return taskSource.Task;
#endif
		}
	}
}
